package __回溯算法

/*
https://leetcode.cn/problems/permutations/

46. 全排列
给定一个不含重复数字的数组 nums ，返回其 所有可能的全排列 。你可以 按任意顺序 返回答案.

示例 1：
输入：nums = [1,2,3]
输出：[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

示例 2：
输入：nums = [0,1]
输出：[[0,1],[1,0]]

示例 3：
输入：nums = [1]
输出：[[1]]
*/
var (
	res46  [][]int
	path46 []int
	used46 []bool // state的缩写
)

func permute(nums []int) [][]int {
	res46, path46 = make([][]int, 0), make([]int, 0, len(nums))
	used46 = make([]bool, len(nums))

	dfs46(nums, 0)
	return res46
}

func dfs46(nums []int, cur int) {
	if cur == len(nums) {
		tmp := make([]int, len(path46))
		copy(tmp, path46)
		res46 = append(res46, tmp)

		return
	}
	for i := 0; i < len(nums); i++ {
		if !used46[i] {
			path46 = append(path46, nums[i])
			used46[i] = true
			dfs46(nums, cur+1)
			used46[i] = false
			path46 = path46[:len(path46)-1]
		}
	}
}
